<!--版权所有 (C) 2025 尋鯨錄 -->
<!--保留所有权利。 -->
<!--博客：https://www.xunjinlu.fun/ -->
<!--如需二开请保留版权，谢谢。 -->
 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单图床</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #4a90e2;
            --secondary-color: #f5f5f5;
            --border-color: #ddd;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            background-color: var(--secondary-color);
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 2rem;
            padding: 1rem;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: relative;
        }

        .header-right {
            position: absolute;
            top: 1rem;
            right: 1rem;
        }

        /* 认证按钮样式 */
        .auth-btn {
            padding: 8px 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 13px;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
            position: relative;
            overflow: hidden;
        }

        .auth-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .auth-btn:hover::before {
            left: 100%;
        }

        .auth-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
        }

        .auth-btn.logged-in {
            background: linear-gradient(135deg, #56ab2f 0%, #a8e6cf 100%);
            box-shadow: 0 2px 10px rgba(86, 171, 47, 0.3);
        }

        .auth-btn.logged-in:hover {
            box-shadow: 0 4px 20px rgba(86, 171, 47, 0.4);
        }

        /* 认证模态框样式 */
        .auth-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.8);
            z-index: 1000;
            backdrop-filter: blur(5px);
        }

        .auth-modal-content {
            position: relative;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            margin: 80px auto;
            padding: 40px;
            max-width: 420px;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            animation: authModalSlideIn 0.4s ease-out;
        }

        @keyframes authModalSlideIn {
            from {
                opacity: 0;
                transform: translateY(-30px) scale(0.9);
            }
            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .auth-modal h3 {
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 10px;
            text-align: center;
            font-weight: 600;
        }

        .auth-modal p {
            color: #7f8c8d;
            text-align: center;
            margin-bottom: 25px;
            font-size: 14px;
        }

        .auth-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .auth-input {
            padding: 15px 20px;
            border: 2px solid #e0e6ed;
            border-radius: 12px;
            font-size: 16px;
            background: white;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .auth-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            transform: translateY(-1px);
        }

        .auth-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 10px;
        }

        .auth-buttons .cancel-btn {
            background: linear-gradient(135deg, #95a5a6 0%, #7f8c8d 100%);
            border-radius: 12px;
            padding: 12px 25px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .auth-buttons .confirm-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
            border-radius: 12px;
            padding: 12px 25px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .auth-buttons button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }

        .header h1 {
            color: var(--primary-color);
            font-size: 2rem;
            margin-bottom: 0.5rem;
        }

        .upload-section {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
        }

        .upload-form {
            border: 2px dashed var(--primary-color);
            padding: 2rem;
            text-align: center;
            border-radius: 10px;
            background: rgba(74, 144, 226, 0.05);
            transition: all 0.3s ease;
        }

        .upload-form:hover {
            border-color: #2779bd;
            background: rgba(74, 144, 226, 0.1);
        }

        .file-input-wrapper {
            margin-bottom: 1rem;
        }

        .custom-file-input {
            display: none;
        }

        .file-input-label {
            display: inline-block;
            padding: 12px 24px;
            background: var(--primary-color);
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .file-input-label:hover {
            background: #2779bd;
        }

        .upload-btn {
            padding: 12px 24px;
            background: #28a745;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1rem;
            transition: background 0.3s ease;
        }

        .upload-btn:hover {
            background: #218838;
        }

        .result {
            background: white;
            padding: 1.5rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
            display: none;
        }

        .url-container {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 1rem;
        }

        .url-input {
            flex: 1;
            padding: 10px;
            border: 1px solid var(--border-color);
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .copy-btn {
            padding: 10px 20px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .copy-btn:hover {
            background: #2779bd;
        }

        .preview {
            max-width: 100%;
            margin-top: 1rem;
            border-radius: 5px;
        }

        .gallery-btn {
            display: block;
            width: 100%;
            padding: 1rem;
            background: var(--primary-color);
            color: white;
            text-align: center;
            text-decoration: none;
            border-radius: 5px;
            transition: background 0.3s ease;
        }

        .gallery-btn:hover {
            background: #2779bd;
        }

        .gallery {
            column-count: 2;
            column-gap: 1rem;
            margin-top: 2rem;
            display: block;
        }

        .gallery-item {
            break-inside: avoid;
            margin-bottom: 1rem;
            position: relative;
            border-radius: 5px;
            overflow: hidden;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .gallery-item:hover {
            transform: scale(1.02);
        }

        .gallery-item img {
            width: 100%;
            height: auto;
            display: block;
            border-radius: 5px;
        }

        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }

            .header h1 {
                font-size: 1.5rem;
            }

            .upload-section {
                padding: 1rem;
            }

            .upload-form {
                padding: 1rem;
            }

            .gallery {
                column-count: 2;
            }
        }

        @media (max-width: 480px) {
            .gallery {
                column-count: 1;
            }
        }

        /* 成功提示样式 */
        .success-message {
            background-color: #d4edda;
            color: #155724;
            padding: 1rem;
            border-radius: 5px;
            margin-bottom: 1rem;
            display: none;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
            z-index: 1000;
            padding: 20px;
            box-sizing: border-box;
        }

        .modal-content {
            position: relative;
            max-width: 800px;
            margin: 40px auto;
            background: white;
            padding: 20px;
            border-radius: 10px;
        }

        .modal-close {
            position: absolute;
            right: 20px;
            top: 20px;
            font-size: 24px;
            color: white;
            cursor: pointer;
            z-index: 1001;
        }

        .modal-image {
            max-width: 100%;
            max-height: 70vh;
            display: block;
            margin: 0 auto;
            border-radius: 5px;
        }

        .modal-info {
            background: white;
            padding: 20px;
            border-radius: 10px;
            margin-top: 20px;
        }

        .modal-url-container {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 10px;
        }

        .modal-url-input {
            flex: 1;
            padding: 10px;
            border: 1px solid var(--border-color);
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .progress-container {
            margin: 15px 0;
            text-align: center;
        }

        .progress-bar {
            width: 100%;
            height: 6px;
            background-color: #f0f0f0;
            border-radius: 3px;
            overflow: hidden;
            margin-bottom: 5px;
        }

        .progress-bar-fill {
            height: 100%;
            background-color: var(--primary-color);
            width: 0;
            transition: width 0.3s ease;
            border-radius: 3px;
        }

        .progress-text {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>简单图床</h1>
            <p>快速上传、分享您的图片</p>
            <div class="header-right">
                <button class="auth-btn" id="authBtn" onclick="toggleAuthModal()">
                    <i class="fas fa-user"></i> <span id="authStatus">登录</span>
                </button>
            </div>
        </div>

        <div class="upload-section">
            <form action="upload.php" method="post" enctype="multipart/form-data" class="upload-form">
                <div class="file-input-wrapper">
                    <input type="file" name="image[]" accept="image/*" required class="custom-file-input" id="fileInput" multiple>
                    <label for="fileInput" class="file-input-label">
                        <i class="fas fa-cloud-upload-alt"></i> 选择图片
                    </label>
                    <div id="fileName"></div>
                </div>
                <div class="progress-container" style="display: none;">
                    <div class="progress-bar">
                        <div class="progress-bar-fill"></div>
                    </div>
                    <div class="progress-text">0%</div>
                </div>
                <button type="submit" class="upload-btn">
                    <i class="fas fa-upload"></i> 上传图片
                </button>
            </form>
        </div>

        <div id="result" class="result">
            <div class="success-message">
                <i class="fas fa-check-circle"></i> 上传成功！
            </div>
            <div class="url-container">
                <input type="text" readonly class="url-input" id="image-url">
                <button class="copy-btn" onclick="copyUrl()">
                    <i class="fas fa-copy"></i> 复制链接
                </button>
            </div>
            <img id="preview" class="preview">
        </div>

        <a href="gallery.html" class="gallery-btn">
            <i class="fas fa-images"></i> 查看已上传图片
        </a>

        <div id="gallery" class="gallery">
            <!-- 图片将通过JavaScript动态加载 -->
        </div>
    </div>

    <div id="imageModal" class="modal">
        <span class="modal-close" onclick="closeModal()">&times;</span>
        <div class="modal-content">
            <img id="modalImage" class="modal-image">
            <div class="modal-info">
                <h3>图片信息</h3>
                <p>文件名: <span id="modalFileName"></span></p>
                <p>图片类型: <span id="modalFileType"></span></p>
                <div class="modal-url-container">
                    <input type="text" readonly class="modal-url-input" id="modalImageUrl">
                    <button class="copy-btn" onclick="copyModalUrl()">
                        <i class="fas fa-copy"></i> 复制链接
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 登录模态框 -->
    <div class="auth-modal" id="authModal">
        <div class="auth-modal-content">
            <span class="modal-close" onclick="closeAuthModal()">&times;</span>
            <div id="loginForm">
                <h3>用户登录</h3>
                <p>请输入管理员密码</p>
                <div class="auth-form">
                    <input type="password" id="passwordInput" placeholder="请输入密码" class="auth-input">
                    <div class="auth-buttons">
                        <button class="cancel-btn" onclick="closeAuthModal()">取消</button>
                        <button class="confirm-btn" onclick="handleLogin()">登录</button>
                    </div>
                </div>
            </div>
            <div id="userInfo" style="display: none;">
                <h3>用户信息</h3>
                <p>您已登录</p>
                <div class="auth-buttons">
                    <button class="confirm-btn" onclick="handleLogout()">退出登录</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 显示选择的文件名
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const fileName = e.target.files[0]?.name || '未选择文件';
            document.getElementById('fileName').textContent = fileName;
        });

        // 复制URL功能
        function copyUrl() {
            const urlInput = document.getElementById('image-url');
            urlInput.select();
            document.execCommand('copy');
            alert('链接已复制到剪贴板！');
        }

        // 检查URL参数并显示上传结果
        const urlParams = new URLSearchParams(window.location.search);
        const imageUrl = urlParams.get('image');
        if (imageUrl) {
            document.getElementById('result').style.display = 'block';
            document.getElementById('image-url').value = imageUrl;
            document.getElementById('preview').src = imageUrl;
            document.querySelector('.success-message').style.display = 'block';
        }

        // 切换图片库显示
        function toggleGallery() {
            const gallery = document.getElementById('gallery');
            if (gallery.style.display === 'none' || gallery.style.display === '') {
                gallery.style.display = 'grid';
                loadGallery();
            } else {
                gallery.style.display = 'none';
            }
        }

        // 加载图片库
        function loadGallery() {
            fetch('list_images.php')
                .then(response => response.json())
                .then(images => {
                    const gallery = document.getElementById('gallery');
                    gallery.innerHTML = '';
                    images.forEach(image => {
                        const item = document.createElement('div');
                        item.className = 'gallery-item';
                        item.innerHTML = `
                            <img src="img/${image}" alt="${image}" onclick="showImageDetails('${image}')">
                        `;
                        gallery.appendChild(item);
                    });
                })
                .catch(error => console.error('Error loading gallery:', error));
        }

        // 添加显示图片详情的函数
        function showImageDetails(imageName) {
            const modal = document.getElementById('imageModal');
            const modalImage = document.getElementById('modalImage');
            const modalFileName = document.getElementById('modalFileName');
            const modalFileType = document.getElementById('modalFileType');
            const modalImageUrl = document.getElementById('modalImageUrl');

            const imageUrl = `${window.location.origin}${window.location.pathname.replace('index.html', '')}img/${imageName}`;
            
            modalImage.src = `img/${imageName}`;
            modalFileName.textContent = imageName;
            modalFileType.textContent = imageName.split('.').pop().toUpperCase();
            modalImageUrl.value = imageUrl;

            modal.style.display = 'block';
        }

        // 添加关闭模态框的函数
        function closeModal() {
            document.getElementById('imageModal').style.display = 'none';
        }

        // 添加复制模态框中链接的函数
        function copyModalUrl() {
            const modalImageUrl = document.getElementById('modalImageUrl');
            modalImageUrl.select();
            document.execCommand('copy');
            alert('链接已复制到剪贴板！');
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('imageModal');
            const authModal = document.getElementById('authModal');
            if (event.target == modal) {
                closeModal();
            }
            if (event.target == authModal) {
                closeAuthModal();
            }
        }

        // 检查认证状态
        function checkAuthStatus() {
            fetch('auth_api.php?action=status')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const authStatus = data.data;
                        if (authStatus.enabled && !authStatus.logged_in) {
                            // 如果认证已启用但用户未登录，显示提示
                            showAuthNotice();
                        }
                    }
                })
                .catch(error => console.error('检查认证状态失败:', error));
        }

        function showAuthNotice() {
            // 先检查是否已经存在提示，避免重复显示
            if (document.querySelector('.auth-notice')) {
                return;
            }
            
            const uploadForm = document.querySelector('.upload-form');
            const notice = document.createElement('div');
            notice.className = 'auth-notice';
            notice.innerHTML = `
                <div style="background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 5px; padding: 15px; margin-bottom: 20px; text-align: center;">
                    <i class="fas fa-exclamation-triangle" style="color: #856404;"></i>
                    <strong style="color: #856404;">需要登录</strong><br>
                    <span style="color: #856404;">请先登录才能上传图片</span><br>
                    <button onclick="toggleAuthModal()" style="background: #4a90e2; color: white; border: none; border-radius: 5px; padding: 8px 16px; cursor: pointer; font-weight: bold;">点击这里登录</button>
                </div>
            `;
            uploadForm.parentNode.insertBefore(notice, uploadForm);
        }

        function hideAuthNotice() {
            const existingNotice = document.querySelector('.auth-notice');
            if (existingNotice) {
                existingNotice.remove();
            }
        }

        // 页面加载完成后检查认证状态
        document.addEventListener('DOMContentLoaded', function() {
            checkAuthStatus();
        });

        // 认证相关函数
        let authStatus = { enabled: false, logged_in: false };

        function checkAuthStatus() {
            fetch('auth_api.php?action=status')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        authStatus = data.data;
                        updateAuthUI();
                        // 根据认证状态显示或隐藏提示
                        if (authStatus.enabled && !authStatus.logged_in) {
                            showAuthNotice();
                        } else {
                            hideAuthNotice();
                        }
                    }
                })
                .catch(error => console.error('检查认证状态失败:', error));
        }

        function updateAuthUI() {
            const authBtn = document.getElementById('authBtn');
            const authStatusSpan = document.getElementById('authStatus');
            
            if (authStatus.enabled) {
                if (authStatus.logged_in) {
                    authBtn.classList.add('logged-in');
                    authStatusSpan.textContent = '已登录';
                } else {
                    authBtn.classList.remove('logged-in');
                    authStatusSpan.textContent = '登录';
                }
            } else {
                authBtn.style.display = 'none';
            }
        }

        function toggleAuthModal() {
            if (!authStatus.enabled) return;
            
            const modal = document.getElementById('authModal');
            const loginForm = document.getElementById('loginForm');
            const userInfo = document.getElementById('userInfo');
            
            if (authStatus.logged_in) {
                loginForm.style.display = 'none';
                userInfo.style.display = 'block';
            } else {
                loginForm.style.display = 'block';
                userInfo.style.display = 'none';
                document.getElementById('passwordInput').value = '';
            }
            
            modal.style.display = 'block';
        }

        function closeAuthModal() {
            document.getElementById('authModal').style.display = 'none';
        }

        function handleLogin() {
            const password = document.getElementById('passwordInput').value;
            
            if (!password) {
                alert('请输入密码');
                return;
            }
            
            fetch('auth_api.php?action=login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ password: password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    closeAuthModal();
                    // 立即更新认证状态和UI
                    authStatus.logged_in = true;
                    updateAuthUI();
                    hideAuthNotice();
                } else {
                    alert('登录失败: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('登录失败，请重试');
            });
        }

        function handleLogout() {
            fetch('auth_api.php?action=logout', {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    closeAuthModal();
                    // 立即更新认证状态和UI
                    authStatus.logged_in = false;
                    updateAuthUI();
                    if (authStatus.enabled) {
                        showAuthNotice();
                    }
                } else {
                    alert('退出失败: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('退出失败，请重试');
            });
        }

        document.querySelector('.upload-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const progressContainer = document.querySelector('.progress-container');
            const progressBarFill = document.querySelector('.progress-bar-fill');
            const progressText = document.querySelector('.progress-text');
            
            // 显示进度条
            progressContainer.style.display = 'block';
            
            // 使用 XMLHttpRequest 来获取上传进度
            const xhr = new XMLHttpRequest();
            
            xhr.upload.addEventListener('progress', function(e) {
                if (e.lengthComputable) {
                    const percent = Math.round((e.loaded * 100) / e.total);
                    progressBarFill.style.width = percent + '%';
                    progressText.textContent = percent + '%';
                }
            });
            
            xhr.addEventListener('load', function() {
                if (xhr.status === 200) {
                    try {
                        const response = JSON.parse(xhr.responseText);
                        if (response.success) {
                            document.getElementById('result').style.display = 'block';
                            document.getElementById('image-url').value = response.url;
                            document.getElementById('preview').src = response.url;
                            document.querySelector('.success-message').style.display = 'block';
                            progressContainer.style.display = 'none';
                        } else {
                            alert(response.error || '上传失败，请重试');
                        }
                    } catch (e) {
                        alert('上传失败，请重试');
                    }
                } else {
                    alert('上传失败，请重试');
                }
            });
            
            xhr.addEventListener('error', function() {
                alert('上传失败，请重试');
            });
            
            xhr.open('POST', 'upload.php', true);
            xhr.send(formData);
        });
    </script>
</body>
</html> 